let Controller = {
	mounted() {
		this.initData();
	},
	activated() {
		this.initData();
		this.requestDetail();
	},
	methods: {
		initData() {
			const param = JSON.parse(this.$route.query.data);
			this.Model.queryData = _.cloneDeep(param);
			this.Model.filterOption2 = _.cloneDeep(this.Model.defaultFilterOption2);
		},
		onSelectChange(type) {
			const data = this.Model.sliderSelect[type];
			this.Model.sliderSelect[type].sliderVal = [data.value, data.value];
		},
		goBack() {
			this.$router.back(-1);
		},
		changeGood() {
			this.getGoodsAttributelist();
		},
		getGoodsAttributelist() {
			//查销售分类     M05
			this.$ajax.post(this.apiTest + `/sale/saleGoodsAttribute/getGoodsAttributelist`, {
					"attributeCode": 'M05'
				})
				.then(res1 => {
					const data1 = res1.data;
					if (data1.status == 200) {
						this.$ajax.post(this.apiTest + `/sale/saleGoodsAttribute/getGoodsAttributelist`, {
							"attributeCode": 'M04'
						}).then(res2 => {
							const data2 = res2.data;
							if (data2.status == 200) {
								this.setFilterOption2(data1.result, data2.result);
							}
						}).catch(err => {})

					}
				}).catch(err => {})
		},
		setFilterOption2(arr1, arr2) {
			this.Model.filterOption2 = _.cloneDeep(this.Model.defaultFilterOption2);
			// this.Model.filterOption2 = this.Model.defaultFilterOption2;
			this.Model.filterOption2.items[0].data = arr1.map(item => {
				return {
					label: item.attributeValueName,
					// value: item.id
					value: item.attributeValueCode
				}
			});
			this.Model.filterOption2.items[1].data = arr2.map(item => {
				return {
					label: item.attributeValueName,
					// value: item.id
					value: item.attributeValueCode
				}
			});
			this.toggleDialog(true);
		},
		findStyleASeriesOf(id) {
			this.$ajax.post(this.apiTest + `/sale/saleGoodsStyle/findStyleASeriesOf/${id}`, {})
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.Model.filterOption2.items[2].data = [];
						this.Model.filterOption2.items[2].data = data.result.map(item => {
							return {
								label: item.modelName,
								value: item.id
							}
						})
					}
				}).catch(err => {})
		},
		findDataFromStyleNumber(id) {
			this.$ajax.get(this.apiTest + `/sale/saleGoodsStyle/condition/${id}`)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						const result = data.result;
						/* description:	M09(颜色)、M10(净度)、M11(切工)、M12(抛光)、M13(对称)、M15(材质)、M17(链长)
						*/
						//材质
						this.Model.filterOption3.items[3].data = result.attrMap.M15.map(item => {
							return {
								label: item.attributeValueName,
								value: item.attributeValueCode
							}
						});
						//手寸
						this.Model.filterOption3.items[4].data = result.handInchList.map(item => {
							return {
								label: item,
								value: item
							}
						});
						//净度
						this.Model.filterOption3.items[5].data = result.attrMap.M10.map(item => {
							return {
								label: item.attributeValueName,
								value: item.attributeValueCode
							}
						});
						//对称
						this.Model.filterOption3.items[6].data = result.attrMap.M13.map(item => {
							return {
								label: item.attributeValueName,
								value: item.attributeValueCode
							}
						});
						//抛光
						this.Model.filterOption3.items[7].data = result.attrMap.M12.map(item => {
							return {
								label: item.attributeValueName,
								value: item.attributeValueCode
							}
						});
						//链长
						this.Model.filterOption3.items[8].data = result.attrMap.M17.map(item => {
							return {
								label: item.attributeValueName,
								value: item.attributeValueCode
							}
						});
						//颜色
						this.Model.filterOption3.items[9].data = result.attrMap.M09.map(item => {
							return {
								label: item.attributeValueName,
								value: item.attributeValueCode
							}
						});
						//切工
						this.Model.filterOption3.items[10].data = result.attrMap.M11.map(item => {
							return {
								label: item.attributeValueName,
								value: item.attributeValueCode
							}
						});
						var weightMin = result.range.fromTheRabbet * 100; //钻重最小值
						var weightMax = result.range.theRabbet * 100; //钻重最小值
						this.Model.weightMin = weightMin;
						this.Model.weightMax = weightMax;
						var weightArr = [];
						for (var i = weightMin; i < weightMax + 1; i++) {
							weightArr.push(i);
						}
						//钻重
						this.Model.sliderSelect = {
							weight: {
								value: '',
								option: weightArr.map(item => {
									return {
										label: item,
										value: item
									}
								}),
								sliderVal: [weightMin, weightMax],

							}
						}
					}
				}).catch(err => {

				})
		},
		toggleDialog(status) {
			if (!status) {
				this.Model.filterOption2.items.forEach(item => {
					item.data = undefined;
				})
			}
			this.Model.changeGoodsDialogFlag = status;
		},

		addSaleGoods() {
			let list = this.Model.tableData;
			let goodsList,orderDetailId;
			if (this.Model.tableData.length <= 1) {
				return;
			} else {
				list = this.Model.tableData[this.Model.tableData.length - 1];
				list.isAddLogo = this.Model.isLog;
				list.isHigh = this.Model.isHighClass;
				list.lettering = this.Model.letteringContent;
				if(list.isHadChange){
					goodsList = [list];
					orderDetailId = this.Model.goodsInfoId;
				}else{
					goodsList = undefined;
					orderDetailId = 0;
				}
			}
			//新增（3.12）
			var arr = [];
			for (var i = 0; i < list.length; i++) {
				var obj = {
					customGoodsVo: list[i].customGoodsVo,
				}
				arr.push(obj);
			}
			const params = {
				orderId: this.Model.queryData.id,
				afterSaleType: this.Model.queryData.afterSaleType, //售中售后类型（10售中变更 20退款 ）
				shipper: this.Model. tableFormInline[4].value,
				receivingGoodsDate: list.expectReceiveTime, //确认收货日期
				isPosPay: 0, //支付方式(0:非pos机/1:pos机)-先写死
				currency: 'CNY', //币种
				isHigh: 0, //	是否允许高配(0:否,1:是)
				count: 1, //新增（3.12）
				goodsList, //新增（3.12）
				originId: this.Model.queryData.originId, //商品的来源id
			};
			this.$ajax
				.put(this.apiTest + '/sale/saleOrder/addSaleGoods/' + params.orderId + '/' + orderDetailId, params)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.$router.push({
							name: `afterSole`,
						})
						this.$message({
							type: 'success',
							message: '已成功'
						});
					} else {
						this.$message.error(data.code);
					}
				}).catch(err => {})
		},
		addReturnDialogShow(flag) {
			if (flag === true) {
				let data = this.getBank();
				if (data == false) return;
			}
			this.Model.editUserDialogFlag = flag;
		},
		addReturn() {
			const data = this.Model.editUserDialogData;
			const params = {
				bankDeposit: data[0].value,
				bankName: data[1].value,
				account: data[2].value,
				id: this.Model.queryData.id
			};
			this.$ajax
				.post(this.apiTest + `/sale/saleOrder/addReturn`, params)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.$message({
							type: 'success',
							message: '已成功'
						});
						this.requestDetail();
					} else {
						this.$message.error(data.msg)
					}
					this.addReturnDialogShow(false);
				})
				.catch(error => {
					this.$message.error('请求错误');
					this.addReturnDialogShow(false);
				});
		},
		setAddGoodList() {
			if (!this.Model.dialogTableSelect) return false;
			let data = _.cloneDeep(this.Model.dialogTableSelect);
			data.productType = '新产品';
			data.optionFlag = 1;
			data.isAddLogo = this.Model.isLog;
			data.isHigh = this.Model.isHighClass;
			data.lettering = this.Model.letteringContent;
			return data;
		},
		addGoodToTable() {
			const select = this.Model.dialogTableSelect;
			if (!select) return;
			const param = this.setAddGoodList();
			param.idCard = this.Model.idCard;
			// const param = _.cloneDeep(this.Model.dialogTableSelect);
			if (param.goodStatusFlag) {
				this.$ajax.get(this.apiTest + `/sale/saleOrder/getNowGoods/${param.id}/${param.idCard}`)
					.then(res => {
						const data = res.data;
						if (data.status == 200) {
							data.result.isHadChange = true;
							data.result.productType = '新产品';
							data.result.isAddLogo = this.Model.isLog;
							data.result.isHigh = this.Model.isHighClass;
							data.result.lettering = this.Model.letteringContent;
							if (data.result) {
								if (this.Model.tableData.length > 1) {
									this.$set(this.Model.tableData,'1',data.result)
								} else {
									this.Model.tableData = this.Model.tableData.concat(data.result);
								}
								this.toggleDialog(false);
							}
						} else {
							this.$message.error(data.msg)
						}
					}).catch(err => {
						this.$message.error(`请求错误`)
					})
			} else {
				this.$ajax.post(this.apiTest + `/sale/saleOrder/getCustomGoods`, param)
					.then(res => {
						const data = res.data;
						if (data.status == 200) {
							data.result.isHadChange = true;
							data.result.productType = '新产品';
							data.result.customGoodsVo = select;
							data.result.isAddLogo = this.Model.isLog;
							data.result.isHigh = this.Model.isHighClass;
							data.result.lettering = this.Model.letteringContent;
							if (data.result) {
								if (this.Model.tableData.length > 1) {
									this.$set(this.Model.tableData,'1',data.result)
								} else {
									this.Model.tableData = this.Model.tableData.concat(data.result);
								}
								this.toggleDialog(false);
							}
						} else {
							this.$message.error(data.msg)
						}
					}).catch(err => {
						this.$message.error(`请求错误`)
					})
			}


		},
		/* toggleDialog(status) {
      this.Model.changeGoodsDialogFlag = status;
    }, */
		requestDetail() {
			const oid = this.Model.queryData.id;
			if (oid) this.$ajax
				.get(this.apiTest + `/sale/onSale/order/getGoodDetail/${oid}`)
				.then(response => {
					const data = response.data;
					const result = data.result;
					if (data.status == 200) {
						this.Model.tableData = result.goodsInfoVos;
						this.Model.goodsInfoId = result.goodsInfoVos[0].id;
						this.$set(this.Model.tableData[0], 'productType', '旧产品');
						this.Model.idCard = result.idCard;
						this.Model.tableFormInline = [{
								label: '订单编号',
								value: data.result.orderNumber,
							},
							{
								label: '原sap销售订单号',
								value: data.result.sapOrderNumber
							},
							{
								label: '下单人',
								value: data.result.orderBuyer
							},
							{
								label: '配送类型',
								value: data.result.deliveryType
							},
							{
								label: '发货平台',
								value: data.result.shipperStore
							},
							{
								label: '销售来源',
								value: data.result.saleOrigin
							},
							{
								label: '销售员',
								value: data.result.saleMan
							}
						];
						this.Model.returnTable = [{
							account: result.account,
							bankDeposit: result.bankDeposit,
							bankName: result.bankName,
						}]
					}
				})
				.catch(error => {
					this.$message.error('请求错误');
				});
		},

		tableDetail(row) {
			this.Model.detailDialogFlag = true;
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showSureBarCode(val) {
			this.Model.searchFromData = val;
		},
		showNotSureBarCodeP1(val, select, opt) {
			if (select.key == 'aSeriesOf') {
				this.findStyleASeriesOf(select.val);
			} else if (select.key == 'styleId') {
				this.findDataFromStyleNumber(select.val);
			}
			this.Model.filterOption2.items = _.cloneDeep(opt);
			this.Model.searchFromData2 = val;
		},
		showNotSureBarCodeP2(val) {
			this.Model.searchFromData3 = val;
		},
		toSearchSureBarCode(val) {},
		toSearchNotSureBarCode(val) {},
		toClear(val) {
			this.Model.searchFromData2 = this.Model.defaultSearchFromData2;
			this.Model.searchFromData3 = this.Model.defaultSearchFromData3;
		},
		toExport(val) {},
		onTabChange(val) {
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === false) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = true;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = false;
			}
		},
		onDialogEdit(flag, val) {
			this.Model.editDialogFlag = flag;
			if (flag === false) return;
			const obj = this.Model[val];
			this.Model.editDialogFormData = _.cloneDeep(this.Model.defaultEditDialogFormData);
			this.getAddress();
		},
		changeAddress(val) {
			if (!val) {
				this.getAddress();
			}
		},
		getAddress() {
			const idCard = this.Model.idCard;
			this.$ajax.post(this.apiTest + `/sale/saleOrder/getAddressByIdCard/${idCard}`, {})
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.Model.addressList = data.result;
					}
				}).catch(err => {})
		},
		updateBaseSaleOrder() {
			const orderId = this.Model.queryData.id;
			const data = {
				...this.Model.editDialogFormData,
				orderId,
				idCard: this.Model.idCard,
			};
			let params;
			if (data.shippingMethod === '1') {
				params = {
					orderId,
					idCard: this.Model.idCard,
					shippingMethod: data.shippingMethod,
					shipper: data.shipper,
					originChannel: data.originChannel,
					salesman: data.salesman,
				}
			} else {
				if (this.Model.editDialogNewAddress == false) {
					const index = this.Model.editDialogItemAddressBoxRadio;
					const list = this.Model.addressList;
					data.recipientName = list[index].name;
					data.recipientPhone = list[index].phone;
					data.detailedAddress = list[index].address;
				}
				params = data;
			}
			this.$ajax.post(this.apiTest + `/sale/saleOrder/updateBaseSaleOrder`, params)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.onDialogEdit(false);
						this.$message({
							type: 'success',
							message: '已成功保存'
						});
						this.requestDetail();
					} else {
						this.$message.error(data.msg);
					}
				}).catch(err => {
					this.$message.error(err);
					this.onDialogEdit(false);
				});
		},

		onSearchSureBarCode() {
			const data = this.Model.filterOption;
			const params = {
				condition: {
					batchNo: data.batchNo || undefined,
					customCoding: data.customCoding || undefined,
					commodityType: "Z001",
				},
			};
			this.$ajax
				.post(this.apiTest + `/sale/saleOrder/getGoodsByCode`, params)
				.then(response => {
					const data = response.data;
					let result = data.result.result;
					if (data.status == 200) {
						result.forEach(item => {
							return item.goodStatusFlag = true;
						});
						this.Model.barCodeTable = result;
						this.Model.pager.params.pageSize = data.pageSize;
						this.Model.pager.params.pageIndex = data.pageIndex;
						this.Model.pager.pageTotal = data.pageTotal;
					}
				})
				.catch(error => {
					this.$message.error('请求错误');
				});
		},
		onSearchNoCode() {
			const slider = this.Model.sliderSelect.weight.sliderVal;
			const params = {
				condition: {
					...this.Model.searchFromData2,
					...this.Model.searchFromData3,
					startInsert: (slider[0] / 100).toFixed(2),
					endInsert: (slider[1] / 100).toFixed(2),
				}
			};
			const condition = params.condition;
			this.$ajax.post(this.apiTest + `/sale/saleOrder/customListBySearch`, params)
				.then(response => {
					const data = response.data;
					let result = data.result.result;
					if (data.status == 200) {
						result.forEach(item => {
							return item.goodStatusFlag = false;
						})
						this.Model.barCodeTable = result;
						this.Model.pager.params.pageSize = data.pageSize;
						this.Model.pager.params.pageIndex = data.pageIndex;
						this.Model.pager.pageTotal = data.pageTotal;
					} else {
						this.$message.error(data.msg);
					}
				}).catch(err => {
					this.$message.error('请求错误');
				})
		}

	}
}
export default Controller;
